package com.eee.xiang.market.service.impl;

import com.eee.xiang.market.beans.OperateResponse;
import com.eee.xiang.market.beans.TUser;
import com.eee.xiang.market.beans.TUserExample;
import com.eee.xiang.market.dao.mapper.TUserMapper;
import com.eee.xiang.market.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service(value = "loginService")
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private TUserMapper userMapper;

    @Autowired
    private HttpSession session;

    @Override
    public OperateResponse login(TUser user) {
        TUserExample example = new TUserExample();
        example.createCriteria().andPhoneEqualTo(user.getPhone()).andPasswordEqualTo(user.getPassword()).andUserRoleEqualTo(user.getUserRole());
        List<TUser> tUsers = userMapper.selectByExample(example);
        if(tUsers == null || tUsers.size() < 1){
            return OperateResponse.failResponse("账号或密码错误！");
        }else{
            session.setAttribute("userInfo",tUsers.get(0));
            return OperateResponse.obtainSuccessResponse(tUsers.get(0));
        }
    }
}
